//
//  QDMetroCodeTableViewController+Swift.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/7.
//  Copyright © 2019 YueWen. All rights reserved.
//

import Foundation

extension QDMetroCodeTableViewController: QDPayTypeDefaultPickerViewDelegate {
    
    /// 绑定新的支付方式
    func bindingNewPayType(in view: QDPayTypeDefaultPickerView){
        view.dismiss()
        router(platform: .payTypeList)
//        ritl_topNavigationPush(QDPayTypeViewController(), animated: true)
    }
    
    
    /// 选择后的回调
    func payTypePickerView(view: QDPayTypeDefaultPickerView, select type: QDPayType, isCurrent: Bool, indexPath: IndexPath) {
     
        if !isCurrent {
            
            self.view.showTextHUD(text: "正在设置默认支付方式")

            QDNetworkingManager.requestChangedDefaultChannel(channelId: /*type.rawValue*/view.signPayTypes[indexPath.row].payChannelId, complete: {
                
                self.view.dismissHUD()
                self.requestAllPayType()
                QDUserInfoModel.share.currentChannel = "当前默认\(type.title)"
                
                //刷新
                NotificationCenter.default.post(name: QDUserInfoStatusDidChangedNotificationName, object: nil)
                
            }) { (msg, errorType) in
                
                self.view.showTextHUD(text: msg)
                
                //默认支付方式回滚
                view.resetOriginPayTypes()
            }
        }
        
        view.dismiss()
    }
}


extension QDMetroCodeTableViewController {
    
    
    /// 绑定成为paytypePickView的响应代理
    @objc func bindingPayTypePickerAction() {
        paytypePickView.typeDelegate = self
        paytypePickView.shouldActionIsCurrent = true
    }
    
    
    /// 弹出支付方式选择器
    @objc func showPayTypePickerView (){
        paytypePickView.show()
    }
    
    
    /// 弹出绑定支付渠道的界面
//    @objc func showDefaultPayTypeSettingView(){
//        ritl_topNavigationPush(QDPayTypeViewController(), animated: true)
//    }
    
    
    //请求支持的支付方式
    @objc func requestAllPayType(){
        
        //如果不存在广告，进行刷新:
        
        
        //如果没有登陆，不进行请求
        if !QDUserStatus.isLogin {
            updateTableHeaderViewWith(.loginStatus) //更新到没有登陆状态
            return
        }
        
        if QDUserStatus.isNFC {
            updateTableHeaderViewWith(.NFC); return
        }
        
        view.showWatingHUD()
        QDNetworkingManager.requestPayTypeList(complete: { (payTypes, isBlack, _) in
            
            self.view.dismissHUD()
            
            // 如果在黑名单
            if isBlack {
                self.updateTableHeaderViewWith(.inBlacklist); return
            }
            
            let bindingPayTypes = payTypes.filter { $0.signStatusType == .binding }
            self.setCodeManagerHasChannelTypes(bindingPayTypes.count > 0)
            
            if bindingPayTypes.count == 0 {
                self.updateTableHeaderViewWith(.outPayType); return
            }
            
            self.paytypePickView.signPayTypes = bindingPayTypes
            
            //对默认方式进行赋值
            let defaultPayTypes = payTypes.filter { $0.defaultChannel }
            
            /// 如果不存在
            if defaultPayTypes.count == 0 {
                 //展示默认绑定默认支付渠道
                self.updateTableHeaderViewWith(.outDefaultChannel); return
            }
            
            /// 等待请求数据
            self.updateTableHeaderViewWith(.none)
            let defaultType = defaultPayTypes.first!
            
            // 更改默认信息
            self.codeHeaderView.typeTitleLabel.text = defaultType.type.title
            self.codeHeaderView.typeImageView.image = defaultType.type.payTypeImage.namedImage
            
            // 如果不是最上层的控制器，不进行切换重启
//            let visibleViewController = self.navigationController?.visibleViewController
            let visibleViewController = (QDTabBarController.sharedInstance().currentViewController as? UINavigationController)?.visibleViewController
            
            // 如果是最上层的控制器
            if (visibleViewController is QDMetroCodeTableViewController || visibleViewController is QDLoginCodeViewController)  {//如果是本身或者登陆界面，就可以认为是
                //开启 需要根据本地记录进行切换
                self.setCurrentCodeManagerWithIsMetro(self.isMetro)
            }
            
        }) { (msg, errorType) in
            
            self.view.dismissHUD()
            self.updateTableHeaderViewWith(.netFail)//失败变更为网络失败状态
        }
    }
    
    
    /// 切换二维码进出站状态
    @objc func switchQrcodeType(){

        view.showTextHUD(text: "正在切换成二维码...",delayHidden: 10)
        
        /// 请求切换成二维码进出站状态
        QDNetworkingManager.request(url: QDCustomerHostUrl + "/user/changeTicketMode", paramteres: ["mode":"02","force":"true"], success: { (msg, data) in
            
            QDUserStatus.isNFC = false
            self.view.dismissHUD()
            //重新请求
            self.requestAllPayType()
            
        }) { (msg, errorType) in
            self.view.showTextHUD(text: msg)
        }
    }
}


extension QDMetroCodeTableViewController {
    
    /// 请求广告位
    @objc func requestAdsAtMetroCodeController(){
        
        QDNetworkingManager.requestStaticAds(success: { (datas) in
            
            self.imageUrls = datas.map { $0.imgUrl }
            self.webUrls = datas.map { $0.url }
            self.shareTitles = datas.map { $0.title }
            self.isBoxs = datas.map { NSNumber(booleanLiteral: $0.isBox) }
            self.boxKeys = datas.map { $0.boxKey }
            
            self.tableView.reloadData()
            
        }, error: nil)
    }
}

extension QDMetroCodeTableViewController {
    
    /// 弹出使用范围的控制器
    @objc func pushToUserFieldController(){
        ritl_topNavigationPush(QDBusLineTableViewController(), animated: true)
//        pushToWebViewController(url: busUserFieldUrl, autoTitle: false, title: "使用范围")
    }
}
